<template lang="html">
  <div class="wrapper">
    <h1>Fixed Table Header</h1>
    <div class="table-header">
      <table>
        <thead>
          <tr>
            <th>Code</th>
            <th>Company</th>
            <th>Price</th>
            <th>Change</th>
            <th>Change %</th>
          </tr>
        </thead>
      </table>
    </div>
    <div class="table-content" ref="content">
      <table>
        <tbody>
          <tr>
            <td>AAC</td>
            <td>AUSTRALIAN COMPANY </td>
            <td>$1.38</td>
            <td>+2.01</td>
            <td>-0.36%</td>
          </tr>
          <tr>
            <td>AAD</td>
            <td>AUSENCO</td>
            <td>$2.38</td>
            <td>-0.01</td>
            <td>-1.36%</td>
          </tr>
          <tr>
            <td>AAX</td>
            <td>ADELAIDE</td>
            <td>$3.22</td>
            <td>+0.01</td>
            <td>+1.36%</td>
          </tr>
          <tr>
            <td>XXD</td>
            <td>ADITYA BIRLA</td>
            <td>$1.02</td>
            <td>-1.01</td>
            <td>+2.36%</td>
          </tr>
          <tr>
            <td>AAC</td>
            <td>AUSTRALIAN COMPANY </td>
            <td>$1.38</td>
            <td>+2.01</td>
            <td>-0.36%</td>
          </tr>
          <tr>
            <td>AAD</td>
            <td>AUSENCO</td>
            <td>$2.38</td>
            <td>-0.01</td>
            <td>-1.36%</td>
          </tr>
          <tr>
            <td>AAX</td>
            <td>ADELAIDE</td>
            <td>$3.22</td>
            <td>+0.01</td>
            <td>+1.36%</td>
          </tr>
          <tr>
            <td>XXD</td>
            <td>ADITYA BIRLA</td>
            <td>$1.02</td>
            <td>-1.01</td>
            <td>+2.36%</td>
          </tr>
        </tbody>
      </table>
    </div>
    <br>
    <a href="javascript:void(0);" @click="download()">下载excel</a>
  </div>
</template>

<script>
export default {
  methods: {
    download () {
      const data_type = 'data:application/vnd.ms-excel'

      let table_div = this.$refs["content"]
      console.log(table_div)
      let table_html = table_div.outerHTML.replace(/ /g, '%20')

      let a = document.createElement('a')
      a.href = data_type + ', ' + table_html
      a.download = 'exported_table_' + Math.floor((Math.random() * 9999) + 1000) + '.xls'
      console.log(a)
      // a.click()
    }
  }
}
</script>

<style lang="scss" scoped>
  body {
    background: -webkit-linear-gradient(left, #25c481, #25b7c4);
    font-family: sans-serif;
  }
  .wrapper {
    background: -webkit-linear-gradient(left, #25c481, #25b7c4);
    margin: 50px;
  }
  ::-webkit-scrollbar {
    width: 6px;
  }
  ::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgbar(0, 0, 0, 0.3);
  }
  ::-webkit-scrollbar-thumb {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
  }
  h1 {
    font-size: 30px;
    color: #fff;
    text-transform: uppercase;
    font-weight: 300;
    text-align: center;
    margin-bottom: 15px;
  }
  table {
    width: 100%;
    table-layout: fixed;
    border: none;
  }
  .table-header {
    background: rgba(255, 255, 255, .3);
  }
  .table-content {
    height: 300px;
    overflow-x: auto;
    margin-top: 0;
    border: 1px solid rgba(255, 255, 255, .3);
  }
  th {
    padding: 20px 15px;
    text-align: left;
    font-weight: 500;
    font-size: 12px;
    color: #fff;
    text-transform: uppercase;
  }
  td {
    padding: 15px;
    text-align: left;
    vertical-align: middle;
    font-weight: 300;
    font-size: 12px;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  }
</style>
